package com.foolishbird.sdk.ui.fragment;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.foolishbird.sdk.core.HttpEngineApi;
import com.foolishbird.sdk.params.FloatGameServerResponseParams;
import com.foolishbird.sdk.params.FloatNoticeResponseParams;
import com.foolishbird.sdk.params.ResponseParams;
import com.foolishbird.sdk.params.bean.FloatGameServer;
import com.foolishbird.sdk.params.bean.FloatNoticeInfo;
import com.foolishbird.sdk.params.bean.State;
import com.foolishbird.sdk.ui.assist.OnFinishActivityListener;
import com.foolishbird.sdk.utils.MyFragmentManger;
import com.foolishbird.sdk.utils.ResUtils;
import com.foolishbird.sdk.utils.WL;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;


public class FloatGameServerFragment extends BaseHttpFragment implements View.OnClickListener {


    private ListView mListview;
    private TextView titleTxt;
    private ImageView backView;

    public static final FloatGameServerFragment newInstance(Bundle bundle) {
        FloatGameServerFragment fragment = new FloatGameServerFragment();
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        super.onCreateView(inflater, container, savedInstanceState);
        int resource = ResUtils.getLayoutIdentifier(getActivity(), "fb_fragment_float_notice");
        View contentView =inflater.inflate(resource,null);
        backView = contentView.findViewById(getViewIdentifier("fb_fragment_login_back"));
        backView.setOnClickListener(this);
        titleTxt = contentView.findViewById(getViewIdentifier("fb_fragment_float_title"));
        titleTxt.setText("游戏开服");
        mListview = contentView.findViewById(getViewIdentifier("float_notice_list"));


        HttpEngineApi.getInstance().getFloatGameServer(requestCallbackHandler(FloatGameServerResponseParams.class));
        return contentView;

    }

    private List<FloatGameServer> myNoticeList;

    @Override
    public void onAsyncRequestResult(int flag, ResponseParams response, Object... params) {
        super.onAsyncRequestResult(flag, response, params);
        if (response != null) {
            int code = response.getState().getCode();
            if (State.SUCCESS == code) {
                if(response instanceof FloatGameServerResponseParams){
                    myNoticeList =((FloatGameServerResponseParams)response).getData().getNotices();

                }
            }
            setAdapter();
        }



        WL.d("FloatNoticeFragment",response.toString());
    }

    private void setAdapter(){
        mListview.setAdapter(new BaseAdapter() {
            @Override
            public int getCount() {
                return myNoticeList==null?0:myNoticeList.size();
            }

            @Override
            public Object getItem(int i) {
                return myNoticeList.get(i);
            }

            @Override
            public long getItemId(int i) {
                return i;
            }

            @Override
            public View getView(int i, View view, ViewGroup viewGroup) {
                ViewHolder holder = null;
                if (view == null) {
                    int resource = ResUtils.getLayoutIdentifier(getContext(), "fb_float_gameserver_item");
                    view = LayoutInflater.from(getContext()).inflate(resource, null);
                    holder = new ViewHolder();
                    holder.titleText = (TextView) view.findViewById(ResUtils.getViewIdentifier(getContext(), "float_game_title"));
                    holder.timeText = (TextView) view.findViewById(ResUtils.getViewIdentifier(getContext(), "float_game_time"));
                    holder.statusText = (TextView) view.findViewById(ResUtils.getViewIdentifier(getContext(), "float_game_status"));
                    view.setTag(holder);
                } else {
                    holder = (ViewHolder) view.getTag();
                }
                FloatGameServer menu =myNoticeList.get(i);
                holder.titleText.setText(menu.getName());
                holder.timeText.setText(menu.getOpenTime());
                try {
                    Date openDate=sdf1.parse(menu.getOpenTime());
                    if (openDate.after(new Date())){
                        holder.statusText.setText("待开启");
                        holder.statusText.setTextColor(Color.parseColor("#19b60e"));
                    }else{
                        holder.statusText.setText("已开启");
                        holder.statusText.setTextColor(Color.parseColor("#979696"));
                    }
                }catch (Exception e){

                }



                return view;
            }
        });
    }

    class ViewHolder {
        TextView titleText;
        TextView timeText;
        TextView statusText;
    }

    SimpleDateFormat sdf1=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    @Override
    public void onClick(View view) {
        if(view ==backView){
            MyFragmentManger.getInstance().goBack(this);
        }
//        OnFinishActivityListener listener = getOnFinishActivityListener();
//        if (listener != null) {
//            listener.doFinishActivity(this);
//        }
    }

    private OnFinishActivityListener getOnFinishActivityListener() {
        if (!isNullContext()) {
            Activity activity = getActivity();
            if (activity instanceof OnFinishActivityListener) {
                return (OnFinishActivityListener) activity;
            }
        }
        return null;
    }
}
